﻿namespace Xms.Abp.Components;

public class DistrictCascader : Cascader
{
    private static List<CascaderNode> Districts;
    [Inject] public HttpApiClient HttpClient { get; set; }
    [Parameter] public string Province { get; set; }
    [Parameter] public EventCallback<string> ProvinceChanged { get; set; }
    [Parameter] public string City { get; set; }
    [Parameter] public EventCallback<string> CityChanged { get; set; }
    [Parameter] public string District { get; set; }
    [Parameter] public EventCallback<string> DistrictChanged { get; set; }
    protected override async Task OnInitializedAsync()
    {
        if (Districts == null)
        {
            var districts = await HttpClient.GetAsync<List<DistrictModel>>("/districts.json");
            Districts = ParseOptions(districts);
        }
        Options = Districts;
        var factory = new EventCallbackFactory();
        SelectedNodesChanged = factory.Create<CascaderNode[]>(this, OnSelectedNodesChanged);
        await base.OnInitializedAsync();
    }

    private List<CascaderNode> ParseOptions(List<DistrictModel> list)
    {
        return list?.Select(r => new CascaderNode
        {
            Label = r.Name,
            Value = r.Name,
            Children = ParseOptions(r.Districts)
        })?.ToList();
    }

    private async void OnSelectedNodesChanged(CascaderNode[] cascaderNodes)
    {
        if (cascaderNodes == null || cascaderNodes.Length <= 0)
        {
            if (District != null)
            {
                District = null;
                if (DistrictChanged.HasDelegate)
                {
                    await DistrictChanged.InvokeAsync(District);
                }
            }
            if (City != null)
            {
                City = null;
                if (CityChanged.HasDelegate)
                {
                    await CityChanged.InvokeAsync(City);
                }
            }
            if (Province != null)
            {
                Province = null;
                if (ProvinceChanged.HasDelegate)
                {
                    await ProvinceChanged.InvokeAsync(Province);
                }
            }
        }
        else if (cascaderNodes == null || cascaderNodes.Length == 1)
        {
            if (District != null)
            {
                District = null;
                if (DistrictChanged.HasDelegate)
                {
                    await DistrictChanged.InvokeAsync(District);
                }
            }
            if (City != null)
            {
                City = null;
                if (CityChanged.HasDelegate)
                {
                    await CityChanged.InvokeAsync(City);
                }
            }
            if (Province != null)
            {
                Province = cascaderNodes[0].Value;
                if (ProvinceChanged.HasDelegate)
                {
                    await ProvinceChanged.InvokeAsync(Province);
                }
            }
        }
        else if (cascaderNodes == null || cascaderNodes.Length == 2)
        {
            if (District != null)
            {
                District = null;
                if (DistrictChanged.HasDelegate)
                {
                    await DistrictChanged.InvokeAsync(District);
                }
            }
            if (City != null)
            {
                City = cascaderNodes[0].Value;
                if (CityChanged.HasDelegate)
                {
                    await CityChanged.InvokeAsync(City);
                }
            }
            if (Province != null)
            {
                Province = cascaderNodes[1].Value;
                if (ProvinceChanged.HasDelegate)
                {
                    await ProvinceChanged.InvokeAsync(Province);
                }
            }
        }
        else
        {
            if (District != null)
            {
                District = cascaderNodes[0].Value;
                if (DistrictChanged.HasDelegate)
                {
                    await DistrictChanged.InvokeAsync(District);
                }
            }
            if (City != null)
            {
                City = cascaderNodes[1].Value;
                if (CityChanged.HasDelegate)
                {
                    await CityChanged.InvokeAsync(City);
                }
            }
            if (Province != null)
            {
                Province = cascaderNodes[2].Value;
                if (ProvinceChanged.HasDelegate)
                {
                    await ProvinceChanged.InvokeAsync(Province);
                }
            }
        }
    }
}

public class DistrictModel
{
    public string Name { get; set; }
    public List<DistrictModel> Districts { get; set; }
}
